package com.ai.web.sendreq;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.ai.web.vo.ICashPhoneReturnMsgV;
import com.ai.web.vo.ICashSerialNumReturnMsgV;
import com.ai.web.vo.ManufacturerV;
import com.ai.web.vo.ModelV;
import com.ai.web.weixin.processor.ScanMsgProcessor;

/**
 * iCash通讯接口类
 * @author fangdong
 * 接口1：http://220.243.6.60:80/ICash/pages/watch/tmlInfos!checkinfo?serialNum=138303A2
 * 接口2：http://220.243.6.60:80/ICash/pages/watch/tmlInfos!checkphone?phone=18520747055
 */
public class ICachUtil {
	private static Logger logger = Logger.getLogger(ScanMsgProcessor.class);
	private static String severIpPort = "220.243.6.60:80";
	/**
	 * 接口1:验证序列号
	 * @param serialNum
	 */
	public static ICashSerialNumReturnMsgV verificationSerialNumber(String serialNum) {
		ICashSerialNumReturnMsgV iCashSerialNumReturnMsgV = new ICashSerialNumReturnMsgV();
		String url = "";
		url = "http://"+severIpPort+"/ICash/pages/watch/tmlInfos!checkinfo?serialNum="+serialNum;
		try {
			String responseEntityContent = RequestApp.request(url, null);
			logger.info("content:"+responseEntityContent);
			/**{"contact":"卿青海","errorMsg":"","maintenanceUser":"吴",
			 * "manufacturer":{"brandName":"广电运通","code":"12","factAddress":"科林路9号","factName":"制造中心","id":"402881eb431d810501431d85f5a60004","status":"1"},
			 * "model":{"brandId":"GRGBANKING","id":"8a81dbfb38da678f0138da9c22530099","modelName":"E22","status":"Y","typeId":"ATM"},"result":1}*/
			JSONObject jsonObj = JSONObject.fromObject(responseEntityContent);
			int result = getInt(jsonObj,"result");
			String contact = getString(jsonObj,"contact");
			String contactPhone = getString(jsonObj,"contactPhone");
			String errorMsg = getString(jsonObj,"errorMsg");
			String maintenanceUser = getString(jsonObj,"maintenanceUser");
			String maintenancePhone = getString(jsonObj,"maintenancePhone");
			String machineAddress = getString(jsonObj,"machineAddress");
			String orgId = getString(jsonObj,"orgid");
			String orgName = getString(jsonObj,"orgName");
			//获取对象厂商
			ManufacturerV manufacturerV = new ManufacturerV();
			JSONObject manufacturer = getJSONObject(jsonObj,"manufacturer");
			String brandName = getString(manufacturer,"brandName");
			String code = getString(manufacturer,"code");
			String factAddress = getString(manufacturer,"factAddress");
			String factName = getString(manufacturer,"factName");
			String manufacturerId = getString(manufacturer,"id");
			String manufacturerStatus = getString(manufacturer,"status");
			manufacturerV.setBrandName(brandName);
			manufacturerV.setCode(code);
			manufacturerV.setFactAddress(factAddress);
			manufacturerV.setFactName(factName);
			manufacturerV.setId(manufacturerId);
			manufacturerV.setStatus(manufacturerStatus);
			
			//获取型号
			JSONObject model = getJSONObject(jsonObj,"model");
			String brandId = getString(model,"brandId");
			String modelid = getString(model,"id");
			String modelName = getString(model,"modelName");
			String modelStatus = getString(model,"status");
			String typeId = getString(model,"typeId");
			ModelV modelV = new ModelV();
			modelV.setBrandId(brandId);
			modelV.setId(modelid);
			modelV.setModelName(modelName);
			modelV.setStatus(modelStatus);
			modelV.setTypeId(typeId);
			
			iCashSerialNumReturnMsgV.setSerialNum(serialNum);
			iCashSerialNumReturnMsgV.setContact(contact);
			iCashSerialNumReturnMsgV.setContactPhone(contactPhone);
			iCashSerialNumReturnMsgV.setResult(result);
			iCashSerialNumReturnMsgV.setErrorMsg(errorMsg);
			iCashSerialNumReturnMsgV.setMaintenanceUser(maintenanceUser);
			iCashSerialNumReturnMsgV.setMaintenancePhone(maintenancePhone);
			iCashSerialNumReturnMsgV.setManufacturerV(manufacturerV);
			iCashSerialNumReturnMsgV.setModelV(modelV);
			iCashSerialNumReturnMsgV.setMachineAddress(machineAddress);
			iCashSerialNumReturnMsgV.setOrgId(orgId);
			iCashSerialNumReturnMsgV.setOrgName(orgName);
			logger.info("ICashSerialNumReturnMsgV:"+iCashSerialNumReturnMsgV.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return iCashSerialNumReturnMsgV;
	}
	
	/**
	 * 接口2:维护人员手机号码验证接口
	 * @param serialNum
	 */
	public static ICashPhoneReturnMsgV verificationPhone(String phone) {
		ICashPhoneReturnMsgV iCashPhoneReturnMsgV = new ICashPhoneReturnMsgV();
		String url = "";
		url = "http://"+severIpPort+"/ICash/pages/watch/tmlInfos!checkphone?phone="+phone;
		try {
			String responseEntityContent = RequestApp.request(url, null);
			logger.info("content:"+responseEntityContent);
			/**{"address":"广州","company":"","errorMsg":"","name":"卿青海","result":1}*/
			JSONObject jsonObj = JSONObject.fromObject(responseEntityContent);
			int result = getInt(jsonObj,"result");
			String errorMsg = getString(jsonObj,"errorMsg");
			String name = getString(jsonObj,"name");
			String company = getString(jsonObj,"company");
			String address = getString(jsonObj,"address");

			iCashPhoneReturnMsgV.setPhone(phone);
			iCashPhoneReturnMsgV.setResult(result);
			iCashPhoneReturnMsgV.setErrorMsg(errorMsg);
			iCashPhoneReturnMsgV.setName(name);
			iCashPhoneReturnMsgV.setAddress(address);
			iCashPhoneReturnMsgV.setCompany(company);
			
			logger.info("ICashSerialNumReturnMsgV:"+iCashPhoneReturnMsgV.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return iCashPhoneReturnMsgV;
	}
	


    /**
     * @param jsonObj
     * @param string
     * @return
     */
    private static JSONObject getJSONObject(JSONObject jsonObj, String param) {
        JSONObject result = null;
        try {
            result = jsonObj.getJSONObject(param);
        }catch(Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    /**
     * @param jsonObj
     * @param string
     * @return
     */
    private static int getInt(JSONObject jsonObj, String param) {
        int result = 0;
        try {
            result = jsonObj.getInt(param);
        }catch(Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    /**
     * 
     * @param o
     * @param param
     * @return
     */
    private static String getString(JSONObject o, String param) {
        String result = null;
        try {
            result = o.getString(param);
        }catch(Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
	
	public static void main(String[] args) {
		//接口1测试
//		String serialNum = "138303A2";
//		verificationSerialNumber(serialNum);
		//接口2测试
		String phone = "13694277849";
		ICashPhoneReturnMsgV verificationPhone = verificationPhone(phone);
		System.out.println(verificationPhone.toString());
		
	}
}
